package com.web.filter;

import com.core.shared.Log;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;

/**
 * Created with IntelliJ IDEA.
 * User: xianfei
 * Date: 16-1-4
 * Time: 下午2:36
 * To change this template use File | Settings | File Templates.
 */
public class CustomInterceptor extends HandlerInterceptorAdapter {


    private final DateTimeFormatter dtf = DateTimeFormatter.ofPattern("HH:mm:ss");
    private final LocalTime start = LocalTime.parse("08:00:00", dtf);
    private final LocalTime end = LocalTime.parse("18:00:00", dtf);

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        LocalTime now = LocalTime.now().withNano(0);
        if (now.isBefore(start)) {
            System.out.println("It's not time to go to work");
            response.sendRedirect("/refuse");
            return false;
        } else if (now.isAfter(end)) {
            System.out.println("It's time to go off work");
            response.sendRedirect("/refuse");
            return false;
        }
        Log.getLogger(this.getClass()).info("It's time for work");
        return true;
    }
}
